﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-30
Description:System_Log.aspx.cs页面代码
=============================================================================	
*/

using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Text;
using FineUI;
using FTchina.SearchQuery;
using FTchina.Library;
using FTchina;
using PWMIS.DataMap.Entity;
using FTchina.DAL;
public partial class System_Log  : BasePage
{
    string ReturnPage = "System_Log.aspx";
    FTchina.SearchQuery.QueryModel qm = new FTchina.SearchQuery.QueryModel();//实例化搜索模型
    string WindowsPage = "System_Logaction.aspx";//弹出窗口的页面地址



    protected void Page_Load(object sender, EventArgs e)
    {
       
        
        if (!IsPostBack)
        {
            btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("至少选择一项！");
            btnNew.OnClientClick = Window1.GetShowReference(WindowsPage, "新增");
            if (qm.WorkPage != ReturnPage)
            {

                qm.WorkPage = ReturnPage;
                qm.Key1 = string.Empty;
            }
            BindGrid(qm);
        }
    }


    #region 绑定列表
    private void BindGrid(QueryModel qm)
    {
        BindGrid(qm,false);
    }

    /// <summary>
    /// 绑定表格
    /// </summary>
    private void BindGrid(QueryModel qm, bool IsToExcel)
    {
        //每页条数
        Grid1.PageSize = 15;

        FTchina.Models.System_Log mSystem_Log = new FTchina.Models.System_Log();

        string Level = qm.Key2 == "ALL" ? "" : qm.Key2;//下拉列表全部的时候就like 空，
        //构造查询条件
        OQLCompare cmp = new OQLCompare(mSystem_Log);
        OQLCompare cmpResult = (cmp.Comparer(mSystem_Log.Message, OQLCompare.CompareType.Like, "%" + qm.Key1 + "%")
            & cmp.Comparer(mSystem_Log.Level, OQLCompare.CompareType.Like, "%" + Level + "%"));


        OQL q = new OQL(mSystem_Log);
        q.Select().Where(cmpResult).OrderBy(mSystem_Log.Id, "desc");//构造查询条件

        int count = EntityQuery<FTchina.Models.System_Log>.QueryList(q, mSystem_Log).Count;//获取分页前的条数
        q.PageWithAllRecordCount = count;
        if (IsToExcel)
        {//如果是导出Excel
             
            FTchina.OfficeHelper.ToExcel.IListToExcel(EntityQuery<FTchina.Models.System_Log>.QueryList(q, mSystem_Log));
            return;
        }

        q = FTchina.Logic.PageLogic.MakeSubPage(q, Grid1, count);//分页      

        List<FTchina.Models.System_Log> list = EntityQuery<FTchina.Models.System_Log>.QueryList(q, mSystem_Log);//分页后绑定数据源

       

        Grid1.RecordCount = count;
        Grid1.DataSource = list;
        Grid1.DataBind();




        if (qm.Key1.Length > 0)
        {
            ttbSearch.Text = qm.Key1;
            ttbSearch.ShowTrigger1 = true;
        }
        if (qm.Key2.Length > 0)
        {
            DDLLevel.SelectedValue = qm.Key2;
        }

    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Grid1_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {

        Grid1.PageIndex = e.NewPageIndex;
        BindGrid(qm);
    }
    /// <summary>
    /// 删除按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach (int row in Grid1.SelectedRowIndexArray)
        {
            sb.Append(Grid1.DataKeys[row][0].ToString());
            sb.Append(",");
        }
        foreach (string ids in sb.ToString().TrimEnd(',').Split(','))
        {

            FTchina.BLL.System_LogManager.DeleteSystem_LogById(Convert.ToInt32(ids));
        }
        BindGrid(qm);
        Grid1.SelectedRowIndexArray = new int[] { };//去除默认的选项
    }
    protected void btnDeleteAll_Click(object sender, EventArgs e)
    {
        FTchina.Logic.SystemLogLocic.DeleteAllSystem_log();
        BindGrid(qm);
    }

    /// <summary>
    /// 弹出窗口返回的处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Window1_Close(object sender, EventArgs e)
    {
        BindGrid(qm);
    }
    #endregion

    #region 搜索过滤
    /// <summary>
    /// 点击搜索上的关闭按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger1Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = false;
        ttbSearch.Text = String.Empty;//清空搜索字符串
        qm.Key1 = String.Empty;//清空搜索条件
        BindGrid(qm);
    }
    /// <summary>
    /// 点击搜索按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ttbSearch_Trigger2Click(object sender, EventArgs e)
    {
        ttbSearch.ShowTrigger1 = true;//显示关闭搜索按钮
        if (ttbSearch.Text.Trim().Length > 0)
        {
            qm.Key1 = ttbSearch.Text.Trim();
        }
        else
        {
            qm.Key1 = "";
        }
        qm.Key2 = DDLLevel.SelectedValue;
        BindGrid(qm);
    }


    #endregion


    #region 导出Excel
    /// <summary>
    /// 带过滤条件的导出，不分页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_click(object sender, EventArgs e)
    {       
        BindGrid(qm, true);       
    }
    #endregion


}






